package com.shangrila.biz.open.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shangrila.biz.domain.TaokeItem;
import com.shangrila.biz.open.TaobaoResult;
import com.shangrila.biz.open.request.TaobaokeItemsCouponRequest;
import com.shangrila.common.lang.Assert;
import com.shangrila.common.lang.Money;
import com.taobao.api.TaobaoResponse;
import com.taobao.api.domain.Item;
import com.taobao.api.domain.ItemImg;
import com.taobao.api.domain.TaobaokeItem;
import com.taobao.api.domain.TaobaokeItemDetail;
import com.taobao.api.request.TaobaokeItemsCouponGetRequest;

public class TaobaoPlatformHelper {

	private final static Logger log = LoggerFactory.getLogger(TaobaoPlatformHelper.class);
	/**
	 * 将response中的错误信息set进result
	 * @param response
	 * @param result
	 */
	public static void doErrorMsg(TaobaoResponse response,TaobaoResult<?> result) {
		if (response == null) {
			throw new NullPointerException("TaobaoResponse is null when invoke TaobaoResultHelper.doErrorMsg");
		}
		result.setSuperCode(response.getErrorCode());
		result.setSuperMessage(response.getMsg());
		result.setCode(response.getSubCode());
		result.setMessage(response.getSubMsg());
		
		StringBuilder sb = new StringBuilder();
		sb.append("TopErrorCode:").append(response.getErrorCode())
				.append(",TopErrorMsg:").append(response.getMsg())
				.append(",TopSubCode:").append(response.getSubCode())
				.append(",TopSubMessage:").append(response.getSubMsg());
		log.error(sb.toString());
	}
	
	public static void doErrorMsg(TaobaoResult<?> result) {
		if (result == null) {
			throw new NullPointerException("TaobaoResult is null when invoke TaobaoResultHelper.doErrorMsg");
		}
		StringBuilder sb = new StringBuilder();
		sb.append("SuperCode:").append(result.getSuperCode())
				.append(",SuperMessage:").append(result.getSuperMessage())
				.append(",SubCode:").append(result.getCode())
				.append(",SubMessage:").append(result.getMessage());
		log.error(sb.toString());
	}
	/**
	 * 把response中的错误在日志文件中打印
	 * @param response
	 */
	public static void doErrorMsg(TaobaoResponse response) {
		if (response == null) {
			throw new NullPointerException("TaobaoResponse is null when invoke TaobaoResultHelper.doErrorMsg");
		}
		StringBuilder sb = new StringBuilder();
		sb.append("TopErrorCode:").append(response.getErrorCode())
				.append(",TopErrorMsg:").append(response.getMsg())
				.append(",TopSubCode:").append(response.getSubCode())
				.append(",TopSubMessage:").append(response.getSubMsg());
		log.error(sb.toString());
	}

	/**
	 * TaobaokeItemDetail --->TaokeItem
	 * @param taobaokeItemDetail
	 * @return
	 */
	public static TaokeItem convert(TaobaokeItemDetail taobaokeItemDetail) {
		if (taobaokeItemDetail == null)
			return null;
		TaokeItem taokeItem = new TaokeItem();
		Item item = taobaokeItemDetail.getItem();
		taokeItem.setItemId(item.getNumIid());
		taokeItem.setMainPicUrl(item.getPicUrl());
		taokeItem.setPicList(getImagesList(item.getItemImgs()));
		taokeItem.setPrice(new Money(item.getPrice()));
		taokeItem.setSellerNick(item.getNick());
		taokeItem.setTaokeUrl(taobaokeItemDetail.getClickUrl());
		taokeItem.setTitle(item.getTitle());
		return taokeItem;
	}
	
	private static List<String> getImagesList(List<ItemImg> itemImgs){
		if(CollectionUtils.isNotEmpty(itemImgs)){
			List<String> list = new ArrayList<String>();
			for(ItemImg itemImg : itemImgs){
				list.add(itemImg.getUrl());
			}
			return list;
		}
		return null;
	}
	
	public static List<TaokeItem> convert(List<TaobaokeItemDetail> taobaokeItemDetailList) {
		if(CollectionUtils.isEmpty(taobaokeItemDetailList)){
			return null;
		}
		List<TaokeItem> items = new ArrayList<TaokeItem>();
		for(TaobaokeItemDetail itemDetail : taobaokeItemDetailList){
			TaokeItem item = new TaokeItem();
			item = convert(itemDetail);
			items.add(item);
		}
		return items;
	}
	
	public static List<TaokeItem> convert(List<TaobaokeItem> taobaokeItemList,List<Item>  itemList){
		if(CollectionUtils.isEmpty(taobaokeItemList) || CollectionUtils.isEmpty(itemList)){
			return null;
		}
		List<TaokeItem> items = new ArrayList<TaokeItem>();
		for(TaobaokeItem item : taobaokeItemList){
			TaokeItem taokeItem = new TaokeItem();
			if(StringUtils.isNotBlank(item.getCouponPrice())){
				taokeItem.setCouponPrice(new Money(item.getCouponPrice()));
			}else{
				taokeItem.setCouponPrice(new Money("0"));
			}
			taokeItem.setItemId(item.getNumIid());
			taokeItem.setPrice(new Money(item.getPrice()));
			taokeItem.setPromotionInItemList(null);
			taokeItem.setPromotionInShopList(null);
			taokeItem.setSellerCreditScore(item.getSellerCreditScore());
			taokeItem.setSellerNick(item.getNick());
			taokeItem.setShopUrl(item.getShopClickUrl());
			taokeItem.setTaokeUrl(item.getClickUrl());
			taokeItem.setTitle(item.getTitle());
			
			for(Item  item2 : itemList){
				if(item2.getNumIid().compareTo(item.getNumIid()) == 0){
					taokeItem.setMainPicUrl(item2.getPicUrl());
					taokeItem.setPicList(getPicUrlList(item2.getItemImgs()));
					break;
				}
			}
			items.add(taokeItem);
		}
		return items;
	}
	/**
	 * 图片转一下
	 * @param itemImgs
	 * @return
	 */
	public static List<String> getPicUrlList(List<ItemImg> itemImgs){
		if(CollectionUtils.isNotEmpty(itemImgs)){
			List<String> list = new ArrayList<String>();
			for(ItemImg img : itemImgs){
				list.add(img.getUrl());
			}
			return list;
		}
		return null;
	}
	
	public static TaobaokeItemsCouponGetRequest convert(TaobaokeItemsCouponRequest request){
		Assert.assertNotNull(request);
		
		TaobaokeItemsCouponGetRequest dest = new TaobaokeItemsCouponGetRequest();
		try {
			BeanUtils.copyProperties(dest, request);
		} catch (Exception e) {
			log.error("BeanUtils.copyProperties catch a exception.",e);
		}
		return dest;
	}
	/**
	 * 将商品id的集合转换成String类型，供开放平台传参用
	 * @param itemids
	 * @return
	 */
	public static String getItemIdsStr(Collection<Long> itemids){
		Assert.assertTrue(itemids !=null && itemids.size()>0);
		StringBuilder sb = new StringBuilder();
		for(Long itemId : itemids){
			sb.append(itemId).append(",");
		}
		if(sb.length()>0){
			return sb.substring(0,sb.length()-1);
		}
		return null;
	}
}
